// pages/card/card.js
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        cards: [],
        token: '',
        host:app.setConfig.url
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {


    },

    callPhone(e){
        let item = e.currentTarget.dataset.item;
        console.log(item)
        let phone = item.phone;
        wx.makePhoneCall({
            phoneNumber:phone
        })
    },

    editCard(e){
        let item = e.currentTarget.dataset.item;
        wx.navigateTo({
            url:'/pages/card/edit?item='+JSON.stringify(item)
        })
    },
    changeDefault(e){
        let item = e.currentTarget.dataset.item;
        let check = e.detail.value
        console.log(item)
    },

    loadCard(){

        var token = app.globalData.token;
        console.log(token)
        let url = app.setConfig.url + "/index.php/Api/Card/index";

        app.postLogin(url,{token},(res)=>{
            let cards = res.data.cards;

            this.setData({
                cards
            });
        })

    },
    addClick() {
        let url = app.setConfig.url + "/index.php/Api/Card/addCard";
        app.postLogin(url, {token: app.globalData.token}, (res) => {
            console.log(res)
            let payInfo = res.data.data
            wx.requestPayment({
                'timeStamp': payInfo.timeStamp,
                'nonceStr': payInfo.nonceStr,
                'package': payInfo.package,
                'signType': 'MD5',
                'paySign': payInfo.paySign,
                success: () => {
                    wx.showToast({
                        title: '支付成功',
                        icon: 'success',
                        duration: 1000
                    })
                    setTimeout(()=>{
                        wx.navigateTo({
                            url:'/pages/card/edit?id='+res.data.id
                        })
                    },1000)
                },
                fail:()=>{
                    wx.showModal({
                        content:"支付失败",
                        showCancel:false
                    })

                }
            });
        })

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.loadCard()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})